<template>
  <div>
    <div class="ccui-multifunctional-table-wrapper">
      <ux-grid
        align="center"
        ref="multipleTable"
        :data="billData"
        :isshow="true"
        border
        max-height="9999"
        class="ccui-multifunctional-table"
        :summary-method="getSummariesBill"
        lazy
        show-summary
        keep-source
        :highlightCurrentRow="false"
        :widthResize="true"
        :edit-config="{ trigger: 'click', mode: 'cell' }"
      >
        <!-- <ux-table-column
          type="checkbox"
          fixed="left"
          width="55"
        ></ux-table-column> -->
        <ux-table-column type="index" title="序号" width="80" fixed="left" />
        <template v-for="(table, index) in tableColumnBill">
          <ux-table-column
            :key="index"
            :field="table.prop"
            :title="table.label"
            :width="table.width"
            :align="table.align"
            :header-align="table.headAlign"
            show-overflow-tooltip
          />
        </template>
      </ux-grid>
    </div>
  </div>
</template>

<script>
import { formatNumber } from "@/basa/utils";
export default {
  props: {
    tableBill: Array
  },
  data() {
    return {
      //   tableData: [],
      tableColumnBill: [
        {
          prop: "storageCode",
          label: "入库单编号",
          width: "180",
          headAlign: "left"
        },
        {
          prop: "storageName",
          label: "入库单名称",
          width: "300",
          headAlign: "left"
        },
        {
          prop: "materialsCode",
          label: "设备物资编码",
          width: "110",
          headAlign: "left"
        },
        {
          prop: "materialsName",
          label: "设备物资名称",
          width: "300",
          headAlign: "left"
        },
        {
          prop: "materialsDescribe",
          label: "设备物资说明",
          width: "200",
          headAlign: "left"
        },
        {
          prop: "currentMeasuringUnit",
          label: "计量单位",
          width: "120",
          headAlign: "left"
        },
        {
          prop: "currentNum",
          label: "数量",
          width: "100",
          headAlign: "left"
        },
        {
          prop: "unitPriceWithoutTax",
          label: "不含税单价",
          width: "130",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "price",
          label: "含税单价",
          width: "130",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "rate",
          label: "税率",
          width: "100",
          formatterName: "rate",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "amountExcludeTax",
          label: "不含税金额",
          width: "130",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "tax",
          label: "税额",
          width: "130",
          formatterName: "tax",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "amount",
          label: "含税金额",
          width: "130",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "remark",
          label: "备注",
          width: "100",
          headAlign: "left"
        }
      ],
      billData: []
    };
  },
  methods: {
    // 发票清单
    getSummariesBill(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (
          ["currentNum", "amount", "amountExcludeTax", "tax"].includes(
            column.property
          )
        ) {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                let num = Number(prev) + Number(curr);
                return num;
              } else {
                return Number(prev);
              }
            }, 0);
            if (index == 11 || index == 12 || index == 13 || index == 14) {
              sums[index] = Number(sums[index]).toFixed(2);
              //   console.log(sums[index])
            }
            if (index == 7 || index == 8) {
               sums[index] = Number(sums[index]).toFixed(3);
            }
          }
        }
      });
      return [sums];
    }
  },
  activated() {
    this.$bus.$on("getTableList", Message => {
      this.tableData = Message.invoceBill;
      this.tableData.forEach(v => {
        v.currentNum = formatNumber(v.currentNum, 3);
        v.unitPriceWithoutTax = Number(v.unitPriceWithoutTax).toFixed(2)
        v.price = Number(v.price).toFixed(2)
        v.rate = new this.$BigNumber(Number(v.rate)).multipliedBy(100) + "%";
        v.amountExcludeTax = Number(v.amountExcludeTax).toFixed(2)
        v.tax = Number(v.tax).toFixed(2)
        v.amount = Number(v.amount).toFixed(2)
      });
    });
  },
  watch: {
    tableBill: {
      handler(n) {
        this.billData = n;
        this.billData.forEach(v => {
          v.currentNum = formatNumber(v.currentNum, 3);
          v.unitPriceWithoutTax = Number(v.unitPriceWithoutTax).toFixed(2);
          v.price = Number(v.price).toFixed(2);
          v.rate = new this.$BigNumber(Number(v.rate)).multipliedBy(100) + "%";
          v.amountExcludeTax = Number(v.amountExcludeTax).toFixed(2);
          v.tax = Number(v.tax).toFixed(2);
          v.amount = Number(v.amount).toFixed(2);
        });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
/deep/ .el-table__fixed-header-wrapper {
  th {
    pointer-events: auto;
  }
}

/deep/ .el-table__fixed,
/deep/.el-table__fixed-right {
  pointer-events: none;
  td {
    pointer-events: auto;
  }
}
</style>
